<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/10/13/013
  Time: 16:43
  To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户管理</title>
    <script src="${pageContext.request.contextPath}/static/js/jquery-1.11.0.min.js"></script>
    <script>
        function bannedUser(uid,name,banned){
            let pageFlag="${sessionScope.pageFlag}";
            //1.弹窗提示
            if(confirm("确定要禁言该用户"+name+"吗?")){
                if (banned=="N"){
                    if ("userListFlag"==pageFlag){
                        location.href= "${pageContext.request.contextPath}/userAdmin/bannedUser?uid="+uid+"&&total=${requestScope.userAdminLimit.getTotal()}&&num=${requestScope.userAdminLimit.getNum()}&&currentPage=${requestScope.userAdminLimit.getCurrentPage()}";
                    }else{
                        location.href= "${pageContext.request.contextPath}/userAdmin/bannedUser?uid="+uid+"&&userInfo=${requestScope.userInfo}";
                    }
                }else {
                    alert("该用户已被禁言！请勿反复操作！");
                }
            }
        };

        function resolveUser(uid,name,banned){
            //1.弹窗提示
            let pageFlag="${sessionScope.pageFlag}";
            if(confirm("确定要为用户"+name+"解除禁言吗?")){
                if (banned=="Y"){
                    if ("userListFlag"==pageFlag){
                        location.href= "${pageContext.request.contextPath}/userAdmin/resolveUser?uid="+uid+"&&total=${requestScope.userAdminLimit.getTotal()}&&num=${requestScope.userAdminLimit.getNum()}&&currentPage=${requestScope.userAdminLimit.getCurrentPage()}";
                    }else{
                        location.href= "${pageContext.request.contextPath}/userAdmin/resolveUser?uid="+uid+"&&userInfo=${requestScope.userInfo}";
                    }
                }else {
                    alert("当前用户未被禁言！无需解禁！");
                }
            }
        };
        function deleteUser(uid,name,state){
            let pageFlag="${sessionScope.pageFlag}";
            //1.弹窗提示
            if(confirm("确定要为用户"+name+"进行封号操作吗?")){
                if (state!="O"){
                    if ("userListFlag"==pageFlag){
                        location.href= "${pageContext.request.contextPath}/userAdmin/deleteUser?uid="+uid+"&&total=${requestScope.userAdminLimit.getTotal()}&&num=${requestScope.userAdminLimit.getNum()}&&currentPage=${requestScope.userAdminLimit.getCurrentPage()}";
                    }else{
                        location.href= "${pageContext.request.contextPath}/userAdmin/deleteUser?uid="+uid+"&&userInfo=${requestScope.userInfo}";
                    }
                }else {
                    alert("该用户已被封号！请勿反复操作！");
                }
            }
        };
        function findComment(userInfo){
                location.href= "${pageContext.request.contextPath}/commentAdmin/findCommentByUserId?userInfo="+userInfo;
        };
        $(function (){
            let pageFlag="${sessionScope.pageFlag}";
            if (pageFlag=="userListFlag"){
                $("#totleDiv").prop('hidden', false);
            }
        });
        $(function (){
            $("#bannedSelected").click(function (){
                var selectFlag = false;
                let pageFlag="${sessionScope.pageFlag}";
                if(confirm("确定要禁言选中的用户吗?")){
                    if ("userListFlag"==pageFlag){
                        $("#form").attr("action","${pageContext.request.contextPath}/userAdmin/bannedSelectUser?total=${requestScope.userAdminLimit.getTotal()}&&num=${requestScope.userAdminLimit.getNum()}&&currentPage=${requestScope.userAdminLimit.getCurrentPage()}");
                    }else{
                        $("#form").attr("action","${pageContext.request.contextPath}/userAdmin/bannedSelectUser?userInfo=${requestScope.userInfo}");
                    }
                    var cbs=document.getElementsByName("uid");
                    for (var i=0;i<cbs.length;i++){
                        if (cbs[i].checked){
                            selectFlag = true;
                            break;
                        }
                    }
                }
                if(selectFlag){
                    $("#form").submit();
                }
            });
            $("#resolveSelected").click(function (){
                var selectFlag = false;
                let pageFlag="${sessionScope.pageFlag}";
                if(confirm("确定要解禁选中的用户吗?")){
                    if ("userListFlag"==pageFlag){
                        $("#form").attr("action","${pageContext.request.contextPath}/userAdmin/resolveSelectUser?&total=${requestScope.userAdminLimit.getTotal()}&&num=${requestScope.userAdminLimit.getNum()}&&currentPage=${requestScope.userAdminLimit.getCurrentPage()}");
                    }else{
                        $("#form").attr("action","${pageContext.request.contextPath}/userAdmin/resolveSelectUser?userInfo=${requestScope.userInfo}");
                    }
                    var cbs=document.getElementsByName("uid");
                    for (var i=0;i<cbs.length;i++){
                        if (cbs[i].checked){
                            selectFlag = true;
                            break;
                        }
                    }
                }
                if(selectFlag){
                    $("#form").submit();
                }
            });
            $("#deletSelected").click(function (){
                var selectFlag = false;
                let pageFlag="${sessionScope.pageFlag}";
                if(confirm("确定要对选中的用户进行封号处理吗?")){
                    if ("userListFlag"==pageFlag){
                        $("#form").attr("action","${pageContext.request.contextPath}/userAdmin/deleteSelectUser?total=${requestScope.userAdminLimit.getTotal()}&&num=${requestScope.userAdminLimit.getNum()}&&currentPage=${requestScope.userAdminLimit.getCurrentPage()}");

                    }else{
                        $("#form").attr("action","${pageContext.request.contextPath}/userAdmin/deleteSelectUser?userInfo=${requestScope.userInfo}");
                    }
                    var cbs=document.getElementsByName("uid");
                    for (var i=0;i<cbs.length;i++){
                        if (cbs[i].checked){
                            selectFlag = true;
                            break;
                        }
                    }
                }
                if(selectFlag){
                    $("#form").submit();
                }
            });
            $("#mixSearchBtn").click(function (){
                var infoFlag = false;
                if ($("#userInfo").val()!=""){
                    infoFlag = true;
                }else {
                    alert("请输入你要用户账号/邮箱/姓名内包含的内容再试！");
                }
                if (infoFlag==true){
                    $("#search").attr("action","${pageContext.request.contextPath}/userAdmin/findUserByUsernameNameEmail");
                    $("#search").submit();
                }

            });
            $("#idSearchBtn").click(function (){
                var infoFlag = false;
                if ($("#userInfo").val()!=""){
                    infoFlag = true;
                }else {
                    alert("请输入用户ID后点击再试！");
                }
                if (infoFlag==true){
                    $("#search").attr("action","${pageContext.request.contextPath}/userAdmin/findUserById");
                    $("#search").submit();
                }

            });
            $("#usernameSearchBtn").click(function (){
                var infoFlag = false;
                if ($("#userInfo").val()!=""){
                    infoFlag = true;
                }else {
                    alert("请输入用户名后点击再试！");
                }
                if (infoFlag==true){
                    $("#search").attr("action","${pageContext.request.contextPath}/userAdmin/findUserByUsername");
                    $("#search").submit();
                }

            });
            $("#emailSearchBtn").click(function (){
                var infoFlag = false;
                if ($("#userInfo").val()!=""){
                    infoFlag = true;
                }else {
                    alert("请输入用户邮箱后点击再试！");
                }
                if (infoFlag==true){
                    $("#search").attr("action","${pageContext.request.contextPath}/userAdmin/findUserByEmail");
                    $("#search").submit();
                }
            });
            $("#first").click(function (){
                //3.1确定全选按钮的选中状态
                var flag = $(this).prop("checked");
                //3.2让所有的checkBox的选中状态 = flag
                $(".first-item").prop("checked",flag);
            });
           $("#nextPageBtn").click(function (){
               if ($("#nextPageBtnLi").prop("class")!="disabled"){
                   currentPage=${requestScope.userAdminLimit.getCurrentPage()}+1;
                   location.href="${pageContext.request.contextPath}/userAdmin/userAdminPageJump?total=${requestScope.userAdminLimit.getTotal()}&&num=${requestScope.userAdminLimit.getNum()}&&currentPage="+currentPage;
               }else {
                   alert("这已经是最后一页了！");
               }

            });
            $(".userPages").click(function (){
                    currentPage=$(this).html();
                    location.href="${pageContext.request.contextPath}/userAdmin/userAdminPageJump?total=${requestScope.userAdminLimit.getTotal()}&&num=${requestScope.userAdminLimit.getNum()}&&currentPage="+currentPage;
            });
            $("#perPageBtn").click(function (){
                if ($("#perPageBtnLi").prop("class")!="disabled"){
                    currentPage=${requestScope.userAdminLimit.getCurrentPage()}-1;
                    location.href="${pageContext.request.contextPath}/userAdmin/userAdminPageJump?total=${requestScope.userAdminLimit.getTotal()}&&num=${requestScope.userAdminLimit.getNum()}&&currentPage="+currentPage;
                }else {
                    alert("这已经是第一页了！");
                }
            });
        });

    </script>
</head>

<body>
<div class="container">
    <div class="row"><c:import url="component/header.jsp"></c:import></div><br><br><br>
    <h1>用户管理系统</h1>
    <div class="row">
        <form id="search" class="navbar-form navbar-left" role="search" action="">
            <div class="alert alert-info alert-dismissible" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <strong>操作提示：</strong> 你要搜索用户名/邮箱/姓名以某一数据开头的用户,请在输入框内输入内容并点击"查询开头"按钮！
            </div>
            <div class="form-group">
                <input type="text" class="form-control" name="userInfo" id="userInfo" placeholder="请输入内容">
            </div>
            <a type="button" id="mixSearchBtn" class="btn btn-default">查询开头</a>
            <a type="button" id="idSearchBtn" class="btn btn-default">ID查询</a>
            <a type="button" id="usernameSearchBtn" class="btn btn-default">用户名查询</a>
            <a type="button" id="emailSearchBtn" class="btn btn-default">邮箱查询</a>
            <a type="button" id="allUser" class="btn btn-default" href="${pageContext.request.contextPath}/admin/userAdmin">查看全部用户</a>
            <a type="button" id="bannedUser" class="btn btn-default" href="${pageContext.request.contextPath}/userAdmin/findBannedUser"; >查看已经禁言用户</a>
            <a type="button" id="deletSelected" class="btn btn-danger">封号选中</a>
            <a type="button" id="bannedSelected" class="btn btn-warning">禁言选中</a>
            <a type="button" id="resolveSelected" class="btn btn-success">解禁选中</a>
        </form>
        <%--<div style="margin-right: 10px">
            <button id="deletSelected" class="btn btn-default">删除选中</button>
            <button id="bannedSelected" class="btn btn-default">禁言选中</button>
            <button id="resolveSelected" class="btn btn-default">解禁选中</button>
        </div>--%>
    </div>
    <form id="form" action="" method="post">
        <table border="1" class="table table-bordered table-hover table-striped">
            <tr class="default">
                <th><input type="checkbox" id="first"></th>
                <th>编号</th>
                <th>姓名</th>
                <%--<th>密码</th>--%>
                <th>禁言</th>
                <th>姓名</th>
                <th>创建时间</th>
                <th>邮箱</th>
                <th>激活状态</th>
                <th>操作</th>
            </tr>
            <%--
               items:从哪个域对象中取出数据 -> list
               varStatus:
                  count:表示循环次数
            --%>
            <c:forEach items="${requestScope.list}" var="user" varStatus="s">
                <tr>
                    <td><input class="first-item" type="checkbox" name="uid" value="${user.uid}"></td>
                    <td>${s.count}</td>
                    <td>${user.username}</td>
                    <%--<td>${user.password}</td>--%>
                    <td>${user.banned}</td>
                    <td>${user.name}</td>
                    <td>${user.create_time}</td>
                    <td>${user.email}</td>
                    <td>${user.active_state}</td>
                    <td>
                        <a class="btn btn-default btn-sm" href="javascript:bannedUser('${user.uid}','${user.name}','${user.banned}')">禁言</a>&nbsp;
                        <a class="btn btn-default btn-sm" href="javascript:resolveUser('${user.uid}','${user.name}','${user.banned}')">解禁</a>
                        <a class="btn btn-default btn-sm" href="javascript:deleteUser('${user.uid}','${user.name}','${user.active_state}')">封号</a>
                        <a class="btn btn-default btn-sm" href="javascript:findComment(${user.uid})">查评论</a>
                    </td>
                </tr>
            </c:forEach>
        </table>
        <div class="col-md-3" style="float: right;font-size: 15px;" id="totleDiv" hidden="hidden">
            <p style="float: right">全站所有用户数据当前共:${requestScope.userAdminLimit.total}条</p>
        </div>
    </form>
    <ul class="pagination" id="userAdminLimit">
        <li id="perPageBtnLi" class="${requestScope.userAdminLimit.hasPre() ? "" : "disabled"}"><a class="button" id="perPageBtn">上一页</a></li>
        <c:forEach items="${requestScope.userAdminLimit.pages}" var="item">
            <li class="${requestScope.userAdminLimit.currentPage == item ? "active" : ""}"><a class="userPages">${item}</a></li>
        </c:forEach>
        <li id="nextPageBtnLi" class="${requestScope.userAdminLimit.hasNext() ? "" : "disabled"}"><a class="button" id="nextPageBtn">下一页</a></li>
    </ul>
</div>

</body>
</html>
